<template>
  <div class="dashboard-content content7">
    <div class="content-wrapper">
      <div class="welcome-card">
        <h3>欢迎回来，{{ userInfo?.nickname || userInfo?.username }}！</h3>
        <div class="user-details">
          <p><strong>用户类型：</strong>{{ getUserTypeText(userInfo?.userType) }}</p>
          <p><strong>角色：</strong>{{ userInfo?.roles?.join(', ') || '无' }}</p>
          <p v-if="userInfo?.lastLoginTime"><strong>上次登录：</strong>{{ formatDateTime(userInfo.lastLoginTime) }}</p>
        </div>
      </div>
      
      <div class="stats-cards">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-card class="stat-card">
              <div class="stat-content">
                <div class="stat-icon">
                  <el-icon size="40" color="#1E90FF"><User /></el-icon>
                </div>
                <div class="stat-info">
                  <h4>用户总数</h4>
                  <p class="stat-number">{{ stats.userCount }}</p>
                </div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="12">
            <el-card class="stat-card">
              <div class="stat-content">
                <div class="stat-icon">
                  <el-icon size="40" color="#E6A23C"><Clock /></el-icon>
                </div>
                <div class="stat-info">
                  <h4>在线用户</h4>
                  <p class="stat-number">{{ stats.onlineCount }}</p>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
      
      <div class="recent-activities">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>最近活动</span>
            </div>
          </template>
          <div class="activity-list">
            <div v-for="activity in recentActivities" :key="activity.id" class="activity-item">
              <div class="activity-icon">
                <el-icon><Bell /></el-icon>
              </div>
              <div class="activity-content">
                <p class="activity-text">{{ activity.description }}</p>
                <p class="activity-time">{{ formatDateTime(activity.createdAt) }}</p>
              </div>
            </div>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed, onMounted } from 'vue'
import { useUserStore } from '@/stores/user'
import { User, Clock, Bell } from "@element-plus/icons-vue"

const userStore = useUserStore()

const userInfo = computed(() => userStore.userInfo)

const stats = reactive({
  userCount: 0,
  onlineCount: 0
})

const recentActivities = ref([
  {
    id: 1,
    description: '用户 admin 登录系统',
    createdAt: new Date().toISOString()
  },
  {
    id: 2,
    description: '系统配置已更新',
    createdAt: new Date(Date.now() - 3600000).toISOString()
  },
  {
    id: 3,
    description: '新用户 user1 注册',
    createdAt: new Date(Date.now() - 7200000).toISOString()
  }
])

const getUserTypeText = (userType: string | undefined) => {
  switch (userType) {
    case 'ADMIN':
      return '管理员'
    case 'USER':
      return '普通用户'
    default:
      return '未知'
  }
}

const formatDateTime = (dateTime: string) => {
  return new Date(dateTime).toLocaleString('zh-CN')
}

const loadStats = async () => {
  try {
    // 模拟数据加载
    stats.userCount = 156
    stats.onlineCount = 23
  } catch (error) {
    console.error('加载统计数据失败:', error)
  }
}

onMounted(() => {
  loadStats()
})
</script>

<style lang="less" scoped>
@import '../styles/variables.less';

.dashboard-content {
  &.content7 {
    // content7 特定样式
    background-color: #f5f7fa;
    min-height: 100%;
  }
}

.content-wrapper {
  max-width: 1200px;
  margin: 0 auto;
}

.welcome-card {
  background: linear-gradient(135deg, @primary-color 0%, #67c23a 100%);
  color: white;
  padding: 30px;
  border-radius: 8px;
  margin-bottom: 20px;
  
  h3 {
    margin: 0 0 15px 0;
    font-size: 24px;
    font-weight: 500;
  }
  
  .user-details {
    p {
      margin: 8px 0;
      opacity: 0.9;
      
      strong {
        font-weight: 500;
      }
    }
  }
}

.stats-cards {
  margin-bottom: 20px;
  
  .stat-card {
    .stat-content {
      display: flex;
      align-items: center;
      padding: 10px 0;
      
      .stat-icon {
        margin-right: 15px;
      }
      
      .stat-info {
        h4 {
          margin: 0 0 5px 0;
          font-size: 14px;
          color: #909399;
          font-weight: normal;
        }
        
        .stat-number {
          margin: 0;
          font-size: 28px;
          font-weight: bold;
          color: #303133;
        }
      }
    }
  }
}

.recent-activities {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    
    span {
      font-weight: 500;
      color: #303133;
      
      @media (max-width: 576px) {
        font-size: 15px;
      }
    }
  }
  
  .activity-list {
    .activity-item {
      display: flex;
      align-items: flex-start;
      padding: 12px 0;
      border-bottom: 1px solid #f0f0f0;
      
      &:last-child {
        border-bottom: none;
      }
      
      @media (max-width: 576px) {
        padding: 10px 0;
      }
      
      .activity-icon {
        margin-right: 12px;
        margin-top: 2px;
        color: @primary-color;
        flex-shrink: 0;
        
        @media (max-width: 576px) {
          margin-right: 8px;
        }
      }
      
      .activity-content {
        flex: 1;
        min-width: 0;
        
        .activity-text {
          margin: 0 0 4px 0;
          color: #303133;
          font-size: 14px;
          word-wrap: break-word;
          
          @media (max-width: 576px) {
            font-size: 13px;
          }
        }
        
        .activity-time {
          margin: 0;
          color: #909399;
          font-size: 12px;
          
          @media (max-width: 576px) {
            font-size: 11px;
          }
        }
      }
    }
  }
}

// 响应式网格布局
@media (max-width: 768px) {
  .el-row {
    margin-left: 0 !important;
    margin-right: 0 !important;
    
    .el-col {
      padding-left: 0 !important;
      padding-right: 0 !important;
      margin-bottom: 10px;
      
      &:last-child {
        margin-bottom: 0;
      }
    }
  }
}

// 1080P 优化
@media (min-width: 1920px) {
  .content-wrapper {
    max-width: 1400px;
  }
  
  .welcome-card {
    padding: 40px;
    
    h3 {
      font-size: 28px;
    }
    
    .user-details p {
      font-size: 16px;
    }
  }
  
  .stats-cards .stat-card .stat-content {
    padding: 25px 20px;
  }
  
  .stats-cards .stat-number {
    font-size: 32px;
  }
}
</style>